<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\model;

use think\Model;

/**
 * 视频点赞 模型
 * @package app\user_center\model
 */
class VideoPlaySupport extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_video_play_support';

    public static function init()
    {
        self::event('after_insert', function ($model) {
            //视频表 点赞数增加
            model('VideoPlay') -> incSupportTimes($model->play_id);
            //用户表 点赞数 增加
            model('user_center/UcMemberCount', 'model\user') -> incPraisedTimes($model->uid);
            //用户表 视频作者 被点赞数 增加
            $authorid = model('VideoPlay') -> find($model->play_id) -> uid;
            model('user_center/UcMemberCount', 'model\user') -> incBePraisedTimes($authorid);
        });
        self::event('after_delete', function ($model) {
            //点赞数减少
            model('VideoPlay') -> decSupportTimes($model->play_id);
            //用户表 点赞数 减少
            model('user_center/UcMemberCount', 'model\user') -> decPraisedTimes($model->uid);
            //用户表 视频作者 被点赞数 减少
            $authorid = model('VideoPlay') -> find($model->play_id) -> uid;
            model('user_center/UcMemberCount', 'model\user') -> decBePraisedTimes($authorid);
        });
    }

    /**
     * 添加数据
     * @param int $uid 用户id
     * @param int $play_id 视频id
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function addData($uid, $play_id, $create_time = ''){
        $video_info = model('video/VideoPlay') -> getPlayFatherPlayInfo($play_id);
		$play_id = $video_info['data'];//关联的视频id	
		
        $data['uid']            = $uid;
        $data['play_id']      	= $play_id;
        if($create_time != '')$data['create_time']    = $create_time;
        $res = $this -> isUpdate(false) -> data($data, true) -> save();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 删除数据
     * @param int $uid 用户id
     * @param int $play_id 视频id
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function deleteData($uid, $play_id){
        //点赞的（长短关联的问题）
        $video_info = model('video/VideoPlay') -> getPlayFatherPlayInfo($play_id);
        $play_id = $video_info['data'];//关联的视频id
        
        $map        = [];
        $map['uid']     = $uid;
        $map['play_id'] = $play_id;
        $res = self::destroy($map);
        \think\facade\Cache::rm('isSupport_'.$uid.'_'.$play_id);
        if($res){

            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 用户是否对某个视频点赞
     * @param int $uid 用户id
     * @param int $page 页码（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function isSupport($uid, $play_id, $cache = true){
        //点赞的（长短关联的问题）
		$video_info = model('video/VideoPlay') -> getPlayFatherPlayInfo($play_id);
		$play_id = $video_info['data'];//关联的视频id	
		
        $map = [];
        $map['uid']     = $uid;
        $map['play_id'] = $play_id;
        $res = $this->cache('isSupport_'.$uid.'_'.$play_id) -> where($map)  -> find();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 获取用户点赞 的列表
     * @param int $uid 用户id
     * @param int $page 页码（默认1）
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
    public function getListByUid($uid){
        
		$map = [];
        $map['video_play_support.uid'] 	= $uid;
        $map['getVideoPlay.play_status'] 			= 1;//视频状态 0禁用 1启用 2测试可见
        $map['getVideoPlay.play_operation_status'] 	= 1;//操作状态 0 待审 1 正常 2拒绝
        $list = self::withJoin('getVideoPlay,getUcMember,getUcMemberCount', 'LEFT') -> where($map) -> order('create_time' ,'desc') -> paginate();
        return array_out(200, 'success', $list);
    }

    /**
     * 获取视频点赞 用户列表
     * @param int $play_id 视频id
     * @param int $page 页码（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function getListByPlayid($play_id, $page = 1){
        
        return '';
    }
	
	/**
     * 用户信息 关联载入（一对一）
     */
    public function getUcMember(){
        return $this -> hasOne('app\user_center\model\user\Member', 'uid', 'uid');
    }
	/**
     * 用户统计 关联载入（一对一）
     */
    public function getUcMemberCount(){
        return $this -> hasOne('app\user_center\model\user\UcMemberCount', 'uid', 'uid');
    }
	/**
     * 视频信息 关联载入（一对一）
     */
	public function getVideoPlay(){
        return $this -> hasOne('app\video\model\VideoPlay', 'id', 'play_id');
    }
}
